# --*--coding=utf-8 --*--
"""
CBS的请求基类
"""
import json
from auto_test.config import CBSConfig
from auto_test.lib.request_util import Request

license_cookie = {
    'JSESSIONID': "UyfyKFSP3jRbYPm6UexCSYFM.632367ff-e81b-306b-9536-dddd2577da80",
    'wersion': 'ie9',
    'projectId': CBSConfig.PROJECT_ID,
    'projectURLRoot': CBSConfig.PROJECT_ROOT_URL,
    'projectURL': CBSConfig.PROJECT_ROOT_URL + CBSConfig.ROUTE_URL,
}

sys_license_cookie = {
    'JSESSIONID': "UyfyKFSP3jRbYPm6UexCSYFM.632367ff-e81b-306b-9536-dddd2577da80",
    'wersion': 'ie9',
    'projectId': CBSConfig.SYSTEM_PROJECT_ID,
    'projectURLRoot': CBSConfig.PROJECT_ROOT_URL,
    'projectURL': CBSConfig.PROJECT_ROOT_URL + CBSConfig.ROUTE_URL,
}


class CBSRequest(Request):
    """
    CBS通用请求方法模拟
    """

    def __init__(self, req_name, data, cookie):
        req_url = CBSConfig.PROJECT_ROOT_URL + CBSConfig.ROUTE_URL + '/' + req_name
        header = {'Content-Type': 'application/x-www-form-urlencoded'}
        super(CBSRequest, self).__init__(req_url, header, data, cookie)

    def post(self, **kwargs):
        """
        发送post请求
        :return:
        """
        if 'application/x-www-form-urlencoded' in self.req_header['Content-Type']:
            self.req_data = json.dumps(self.req_data, ensure_ascii=False).encode()
            return super(CBSRequest, self).post(**kwargs)


def get_user_cookie(user_name='0001'):
    """
    目前只能实现获取密码=1111qqqq用户的登录后的cookie
    :param user_name: 用户的登录名
    :return:
    """
    req_name = 'MBUSLGON.do'
    data = {"USUSRLGNX": [
        {"LGNTYP": "N",
         "LGNNAM": user_name,
         "LNPWDX": "f42C9RvE*fhFz1qcA3e2WiIsLGKU8ngq6SvCjf5kAr2VrYq0v*UV873y8UPpz02XqlmIV*hBniDN9GcwRn-"
                   "30DBgD*U4peFT2Ul4yzijblZ4ERNui-J-H710mR3MV36kYcDuIX4eVwNSTkaGubjCcX2X9sdOlqXtP2bXFLO7l3w",
         "LNPWDY": "Y",
         "PSMSIZ": "1920*1080"}
    ],
        "SYCOMSRCX": [{"VRFCOD": "4276"}],
        "SYCOMREQX": [{"GRPIDT": CBSConfig.PROJECT_ID, "CLTVER": "5.0.0.0"}]
    }
    # 默认登录的cookie，若是9999用户则自动切换到别的证书
    login_cookie = license_cookie
    if user_name == '9999':
        login_cookie = sys_license_cookie
    cbs_request = CBSRequest(req_name, data=data, cookie=login_cookie)
    resp = cbs_request.post(show_log_flag=False)
    user_cookie = license_cookie
    for item in resp.cookies.items():
        user_cookie[item[0]] = item[1]
    return user_cookie


def system_user_login():
    """

    :return:
    """
    req_name = 'MBUSLGON.do'
    data = {"USUSRLGNX": [
        {"LGNTYP": "N",
         "LGNNAM": '9999',
         "LNPWDX": "f42C9RvE*fhFz1qcA3e2WiIsLGKU8ngq6SvCjf5kAr2VrYq0v*UV873y8UPpz02XqlmIV*hBniDN9GcwRn-"
                   "30DBgD*U4peFT2Ul4yzijblZ4ERNui-J-H710mR3MV36kYcDuIX4eVwNSTkaGubjCcX2X9sdOlqXtP2bXFLO7l3w",
         "LNPWDY": "Y",
         "PSMSIZ": "1920*1080"}
    ],
        "SYCOMSRCX": [{"VRFCOD": "4276"}],
        "SYCOMREQX": [{"GRPIDT": CBSConfig.SYSTEM_PROJECT_ID, "CLTVER": "5.0.0.0"}]
    }
    cbs_request = CBSRequest(req_name, data=data, cookie=sys_license_cookie)
    resp = cbs_request.post(show_log_flag=True)
    user_cookie = license_cookie
    for item in resp.cookies.items():
        user_cookie[item[0]] = item[1]
    return user_cookie


# 测试函数或者方法
if __name__ == '__main__':
    system_user_login()
